﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Linq;
using Styx;
using Styx.Helpers;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Inventory;
using Styx.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.WoWInternals.World;
using Styx.Plugins;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;

namespace BuddyManager
{
    public partial class BuddyManager : HBPlugin
    {
        #region Settings

        public static string SavePath
        {
            get { return string.Format("{0}\\BuddyManager\\{1}-{2}\\TaskList.xml", Settings.SettingsDirectory, StyxWoW.Me.Name, Lua.GetReturnVal<string>("return GetRealmName()", 0)); }
        }

        
        public static void LoadSettings()
        {
            if (!File.Exists(SavePath))
                return;

            var xml = XElement.Load(SavePath);

            // Loading settings first.
            var settingsXml = xml.Element("Settings");

            if (settingsXml != null)
            {
                var logoutAfter = settingsXml.Element("LogoutAfter");

                if (logoutAfter != null)
                    uSettings.LogOutAfter = Convert.ToBoolean(logoutAfter.Value);

                var totalRunTime = settingsXml.Element("TotalRunTime");

                if (totalRunTime != null)
                {
                    uSettings.TotalMin = Convert.ToInt32((Double.Parse(totalRunTime.Value) % 60));
                    uSettings.TotalHr = Convert.ToInt32((Double.Parse(totalRunTime.Value) - (Double.Parse(totalRunTime.Value) % 60)) / 60);
                }

                var vendorOnHearth = settingsXml.Element("VendorOnHearth");

                if (vendorOnHearth != null)
                    uSettings.Vendor = Convert.ToBoolean(vendorOnHearth.Value);

                var mailOnHearth = settingsXml.Element("MailOnHearth");

                if (mailOnHearth != null)
                    uSettings.Mail = Convert.ToBoolean(mailOnHearth.Value);

                var forceNext = settingsXml.Element("ForceNextProfile");

                if (forceNext != null)
                    uSettings.ForceNext = Convert.ToBoolean(forceNext.Value);

                var randomize = settingsXml.Element("Randomized");

                if (randomize != null)
                    uSettings.Randomize = Convert.ToBoolean(randomize.Value);

                var shortBreak = settingsXml.Element("ShortBreak");

                if (shortBreak != null)
                    uSettings.shortBreak = Convert.ToBoolean(shortBreak.Value);

            }
            TaskList.Clear();
            var items = xml.Element("Tasks");
            foreach (var x in items.Elements("Task"))
            {
                TaskStruct tmpTask = new TaskStruct();
                tmpTask.BotBase = x.Element("BotBase").Value;
                tmpTask.Zone = x.Element("Zone").Value;
                tmpTask.SwitchOn = x.Element("SwitchOn").Value;
                tmpTask.SwitchInt = int.Parse(x.Element("SwitchInt").Value);
                var subProfs = x.Element("Profiles");
                foreach (var i in subProfs.Elements("Profile"))
                {
                    tmpTask.ProfileList.Add(i.Value);
                }
                TaskList.Add(tmpTask);
            }
        }

        public static void SaveSettings()
        {
            // ReSharper disable AssignNullToNotNullAttribute
            if (!Directory.Exists(Path.GetDirectoryName(SavePath)))
                Directory.CreateDirectory(Path.GetDirectoryName(SavePath));
            // ReSharper restore AssignNullToNotNullAttribute
            var xml = new XElement("BuddyManagerSettings",
                            new XElement("Settings",
                                new XElement("LogoutAfter", uSettings.LogOutAfter),
                                new XElement("TotalRunTime", ((uSettings.TotalHr * 60) + uSettings.TotalMin) ),
                                new XElement("VendorOnHearth", uSettings.Vendor),
                                new XElement("MailOnHearth", uSettings.Mail),
                                new XElement("ForceNextProfile", uSettings.ForceNext),
                                new XElement("Randomized",uSettings.Randomize),
                                new XElement("ShortBreak",uSettings.shortBreak),
                                new XElement("NumberOfTasks", TaskList.Count -1)
                                ),
                            new XElement("Tasks",
                                from i in TaskList
                                select TaskStruct.ToXml(i)
                                    ));
            xml.Save(SavePath);
        }
        #endregion

        /*public XElement AddList(string ListName)
        {
            XElement retXElement = new XElement("BotBase");
            foreach (string s in TaskList[CurrentTaskIndex].BotBase)
            {
                retXElement.Add(s);
            }
            return retXElement;
        }*/
    }

    public static class Extensions
    {
        public static XElement ToXml(this string EleName, string str)
        {
            return new XElement(str, EleName);
        }

        public static XElement ToXml(this int EleDbl, string str)
        {
            return new XElement(str, EleDbl.ToString());
        }

        public static bool IsJustAboveWater(this WoWUnit unit)
        {
            if (Styx.WoWInternals.World.GameWorld.TraceLine(unit.Location, new WoWPoint(unit.Location.X, unit.Location.Y, unit.Location.Z - 5), GameWorld.CGWorldFrameHitFlags.HitTestLiquid) || Styx.WoWInternals.World.GameWorld.TraceLine(unit.Location, new WoWPoint(unit.Location.X, unit.Location.Y, unit.Location.Z - 5), GameWorld.CGWorldFrameHitFlags.HitTestLiquid2))
                return true;
            else
                return false;
        }

    }
}
